package com.example.everying.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.mapper.user.ActivityParticipationMapper;
import com.example.everying.model.entity.user.ActivityParticipation;
import com.example.everying.model.entity.user.ActivityPointsConfig;
import com.example.everying.service.user.ActivityParticipationService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ActivityParticipationServiceImpl extends ServiceImpl<ActivityParticipationMapper, ActivityParticipation> implements ActivityParticipationService {
    /**
     * @param userId       用户ID
     * @param pointsConfig 积分配置
     * @param status       状态
     */
    @Override
    public void add(String userId, ActivityPointsConfig pointsConfig, String status) {
        ActivityParticipation participation = new ActivityParticipation();
        participation.setUserId(userId);
        participation.setActivityId(pointsConfig.getId());
        participation.setParticipationType(pointsConfig.getType());
        participation.setStatus(status);
        participation.setParticipationTime(LocalDateTime.now());
        participation.setCreateTime(LocalDateTime.now());
        participation.setUpdateTime(LocalDateTime.now());
        save(participation);
    }

    /**
     * @param applyId
     * @param firstUserAcceptPoints
     * @return
     */
    @Override
    public ActivityParticipation getRewardConfigByType(String applyId, String firstUserAcceptPoints) {
        QueryWrapper<ActivityParticipation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", applyId);
        queryWrapper.eq("participation_type", firstUserAcceptPoints);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        ActivityParticipation participation = getOne(queryWrapper);
        return participation;
    }
}